package com.example.id;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.qcloud.cos.model.transform.SelectObjectContentEventUnmarshaller;

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

public class IDDemo {

    static CopyOnWriteArraySet<String> copyOnWriteArraySet = new CopyOnWriteArraySet<>();

    public static void main(String[] args) {
        Instant first = Instant.now();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        Snowflake snowflake2 = IdUtil.getSnowflake(1, 1);
        System.out.println(snowflake==snowflake2);
        long id = snowflake.nextId();
        Instant second = Instant.now();
        System.out.println(Duration.between(first,second).toMillis());
        String idStr = snowflake.nextIdStr();
        System.out.println(Duration.between(second,Instant.now()).toMillis());
        System.out.println(id);
        System.out.println(idStr);
    }

    private void testID(){
        for(int i = 0;i<10000;i++){
            String id = IDDemo.generateUUID();

            new Thread(()->{
                copyOnWriteArraySet.add(id);
                System.out.println(copyOnWriteArraySet.size());
            }).start();
        }
    }

    public static String generateUUID(){
        long id = IdUtil.getSnowflakeNextId();
        String idStr = Long.toString(id);
        char[] idChar = idStr.toCharArray();
        char[] newIdChar = new char[idChar.length];
        Random random = new Random();
        for(int i =0;i<idChar.length;i++){
            char c = idChar[i];
            int n = c+49;
            newIdChar[i]=(char) n;
        }
        newIdChar[3] = (char) (newIdChar[3] & 0xDF);
        newIdChar[7] = (char) (newIdChar[7] & 0xDF);
        newIdChar[11] = (char) (newIdChar[11] & 0xDF);
        newIdChar[13] = (char) (newIdChar[13] & 0xDF);
        newIdChar[17] = (char) (newIdChar[17] & 0xDF);
        for(int i = 0;i<3;i++){
            int index = getRandom(random,1,18);
            char c =  (char) (getRandom(random,48,57));
            newIdChar =insert(newIdChar,index,c);
        }
        return "_"+new String(newIdChar);
    }

    /**
     * 获取指定范围内的随机数
     * @param random 随机数对象
     * @param min   最小值
     * @param max   最大值
     * @return
     */
    private static int getRandom(Random random,int min,int max){
        int num = random.nextInt(max)%(max-min+1)+min;
        return num;
    }

    /**
     * 数组插入
     * @param srcChar 原始数组
     * @param index 插入索引
     * @param c 插入字符串
     * @return
     */
    private static char[] insert(char[] srcChar,int index,char c){
        char[] destChar = new char[srcChar.length+1];
        System.arraycopy(srcChar,0,destChar,0,index);
        destChar[index] = c;
        System.arraycopy(srcChar,index,destChar,index+1,srcChar.length-index);
        return destChar;
    }
}

class AddId implements Runnable{

    Set<String> idSet = new HashSet<>();

    private volatile boolean  validate = true;

    @Override
    public void run() {
        String id = IDDemo.generateUUID();
        while(validate){
            idSet.add(id);
            validate = false;
        }
        System.out.println(idSet.size());

    }
}